﻿using BYSJ.Web.Filters;
using BYSJ.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BYSJ.IBLL;
using BYSJ.BLL;
using BYSJ.Model;
using BYSJ.Web.Commmend;
namespace BYSJ.Web.Controllers
{
    [AdminFilter]
    [ShenFen]
    [huDongcebian]
    public class huDongController : Controller
    {
      
        // GET: /huDong/
        BieBiao HuDongModel = new BieBiao();
        IAnswerSetService AnswerServer = null;
        IQuestingSetService QuestingServer = null;
        IUserSetService UserService = null;
        IHuiFuSetService HuiFuService = null;
        AnswerSet answer = new AnswerSet();
        QuestingSet questing = new QuestingSet();
        List<AnswerSet> dbAnsweer = null;
        List<QuestingSet> dbquesting = null;
        AnswerViewModel answerviewmodel = new AnswerViewModel();
        HuiFuSet huifu = new HuiFuSet();
        public huDongController()
        {
            AnswerServer = new AnswerSetService();
            QuestingServer = new QuestingSetService();
            HuiFuService = new HuiFuSetService();
            UserService = new UserSetService();
        }
        /// <summary>
        /// 显示提问页面
        /// </summary>
        /// <param name="user">name字段</param>
        /// <returns></returns>
        public ActionResult HuDong(string Name, string pageindex)
        {
            int totalCount = 0;
            //如果查询的结果为0则显示空视图
            dbAnsweer = AnswerServer.LoadPageEntities(12,Convert.ToInt32(pageindex), out totalCount, u => u.isdel == false, false, a => a.creattime).ToList();
            if (dbAnsweer.Count == 0)
            {
                HuDongModel.name = Name;
                return View("~/Views/huDong/HuDongEmpty.cshtml", HuDongModel);
            }
             //如果不为空 则默认将第一页数据插入
            else
            {
                HuDongModel.hudonglist= Commend.ListConvert(dbAnsweer);
                HuDongModel.name = Name;
                HuDongModel.pageindex = Convert.ToInt32(pageindex);
                //一页显示几条数据
                Double totallCount = (Double)totalCount  / 12;
                HuDongModel.pagcount = Convert.ToInt32(Math.Ceiling(totallCount));
                HuDongModel.pagemax = HuDongModel.pageindex + 5;
                HuDongModel.pagemin = HuDongModel.pageindex - 5;
                if(HuDongModel.pagemax>HuDongModel.pagcount)
                {
                    HuDongModel.pagemax = HuDongModel.pagcount+1;
                    HuDongModel.pagemin = HuDongModel.pagcount - 10;
                }
                if (HuDongModel.pagemin <=0)
                {
                    HuDongModel.pagemax = HuDongModel.pagcount;
                    HuDongModel.pagemin = 0;
                }
                HuDongModel.demo = "我是例子";
                return View("~/Views/huDong/HuDonghuifu.cshtml", HuDongModel);
            }
        }
        /// <summary>
        /// 处理提问请求 关闭请求数据验证
        /// </summary>
        /// <param name="answer">提问内容</param>
        /// <returns></returns>
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult addAnswer(string title, string content)
        {
            if (title.Length==0 || content.Length==0)
            {
                return Content("<script>alert('失败！！标题或内容不能为空21！！');history.go(-1);</script>");
            }
            else
            {
                answer.title = title;
                answer.content = content;
                answer.creattime = DateTime.Now;
                answer.isdel = false;
                answer.uptime = DateTime.Now;
                answer.userid = Convert.ToInt32(Request.Cookies["cookiename"].Value);
                AnswerServer.Add(answer);
                return Content("<script>alert('提问成功！！');location.replace(document.referrer);;</script>");
            }
        }
        ///<param>处理单个问题</param>
        /// </summary>
        /// <param name="Name">Name是User表的Name</param>
        /// <param name="Id">Id是answeer表的id</param>
        /// <returns></returns>
        public ActionResult showAnswer(String Name, string Id)
        {
            int idbiaoshi = Convert.ToInt32(Id);
            dbquesting = QuestingServer.LoadEntities(u => u.answerid == idbiaoshi && u.isdel == false).ToList();
            answer = AnswerServer.LoadEntities(u => u.id == idbiaoshi && u.isdel == false).ToList().First();
            answerviewmodel.name = Name;
            answerviewmodel = Commend.ListAQConvert(dbquesting, answer);
            if (dbquesting.Count == 0)
            {
                return View("~/Views/huDong/emptyAnswer.cshtml", answerviewmodel); 
            }
            else
            {
                return View("~/Views/huDong/showAnswer.cshtml", answerviewmodel);
            }
        }
        /// <summary>
        /// 增加questing表
        /// </summary>
        public ActionResult addQuesting(int answerid, string content)
        {
            if (content.Length == 0)
            {
                return Content("<script>alert('失败！！回答内容不能为空');history.go(-1);</script>");
            }
            else
            {
                 questing.answerid = answerid;
                 questing.content = content;
                 questing.creattime = DateTime.Now;
                 questing.isdel = false;
                 questing.uptime = DateTime.Now;
                 questing.userid = Convert.ToInt32(Request.Cookies["cookiename"].Value);
                 QuestingServer.Add(questing);
                 return Content("<script>alert('回答成功！！');location.replace(document.referrer);;</script>");
            }
        }
        /// <summary>
        /// 增加子回复
        /// </summary>
        /// <param name="questingid">questing表的id</param>
        /// <param name="content">回复内容</param>
        /// <returns></returns>
        public ActionResult huifuAdd(int questingid,string content)
        {
            if (content.Length == 0)
            {
                return Content("<script>alert('失败！！回答内容不能为空');history.go(-1);</script>");
            }
            else
            {
                huifu.content = content;
                huifu.creattime = DateTime.Now;
                huifu.isdel = false;
                huifu.questringid = questingid;
                huifu.uptime = DateTime.Now;
                huifu.userid = Convert.ToInt32(Request.Cookies["cookiename"].Value);
                HuiFuService.Add(huifu);
                return Content("<script>alert('回答成功！！');location.replace(document.referrer);;</script>");
            }
        }
    }
}
